package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect //表示是一个切片类

public class Advice {
    //切入点
    @Pointcut("execution(* com.itheima.service.StudentService.selectAll(..))")
    public void pt(){}

    //定义环绕通知方法
    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp){
        Object result = null;
        try {
            //记录开始时间
            long start = System.currentTimeMillis();
            //执行目标方法
            result = pjp.proceed();
            //记录结束时间
            long end = System.currentTimeMillis();
            //获取方法名称
            String name = pjp.getSignature().getName();
            System.out.println(name+"方法耗时"+(end-start)+"毫秒");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return result;

    }

}
